package com.javasm.service.impl;

import com.javasm.dao.RoomCategoryDao;
import com.javasm.entity.RoomCategory;
import com.javasm.service.RoomCategoryService;
import com.javasm.utils.MyBatisHeapler;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoomCategoryServiceImpl implements RoomCategoryService {
    @Override
    public List<RoomCategory> listAllCategory(RoomCategory inputCategory, Integer page, Integer pageSize) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        RoomCategoryDao roomCategoryDao = sqlSession.getMapper(RoomCategoryDao.class);
        List<RoomCategory> list = roomCategoryDao.listAllCategory(inputCategory, (page - 1) * pageSize, pageSize);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return list;
    }
    @Override
    public Integer countCategory(RoomCategory inputCategory) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        RoomCategoryDao roomCategoryDao = sqlSession.getMapper(RoomCategoryDao.class);
        Integer count = roomCategoryDao.countCategory(inputCategory);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return count;
    }
    @Override
    public Integer addCategory(RoomCategory inputCategory) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        RoomCategoryDao roomCategoryDao = sqlSession.getMapper(RoomCategoryDao.class);
        Integer resNum = roomCategoryDao.addCategory(inputCategory);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }
    @Override
    public Integer deleteCategoryById(Integer categoryId) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        RoomCategoryDao roomCategoryDao = sqlSession.getMapper(RoomCategoryDao.class);
        Integer resNum = roomCategoryDao.deleteCategoryById(categoryId);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }
    @Override
    public Integer editCategory(RoomCategory inputCategory) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        RoomCategoryDao roomCategoryDao = sqlSession.getMapper(RoomCategoryDao.class);
        Integer resNum = roomCategoryDao.editCategory(inputCategory);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }
    @Override
    public RoomCategory getCategoryById(Integer categoryId) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        RoomCategoryDao roomCategoryDao = sqlSession.getMapper(RoomCategoryDao.class);
        RoomCategory roomCategory = roomCategoryDao.getCategoryById(categoryId);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return roomCategory;
    }
    @Override
    public List<RoomCategory> listCategoryForSelect(Integer parentId) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        RoomCategoryDao roomCategoryDao = sqlSession.getMapper(RoomCategoryDao.class);
        List<RoomCategory> list = roomCategoryDao.listCategoryLevel(parentId);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return list;
    }

}
